//
//  CC_TopList_CollectionViewCell.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//



#import "CC_TopList_CollectionViewCell.h"  // 榜单
#import "CCBaseCollectionView.h"
#import "CC_Custom_CollectionReusableView.h"  // 头视图
#import "CC_ClassifyHugeImage_CollectionViewCell.h"  // 图片cell
#import "CC_TopListFocusImage_Model.h"

#define IMAGECELL @"CC_ClassifyHugeImage_CollectionViewCell"


@interface CC_TopList_CollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) CCBaseCollectionView *collectionView;
@property(nonatomic, retain) CC_TopListFocusImage_Model *focusImageModel;

@end



@implementation CC_TopList_CollectionViewCell

- (void)dealloc
{
    [_focusImageModel release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self handleData];
        [self createCollectionView];
        
    }
    return self;
}
-(void)handleData{
    [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
        NSDictionary *dic = result;
        NSDictionary *focusImages = [dic objectForKey:@"focusImages"];
        NSLog(@"%@",focusImages);
        NSArray *list = [focusImages objectForKey:@"list"];
        NSDictionary *modelDic = list.firstObject;
        self.focusImageModel = [[CC_TopListFocusImage_Model alloc]initWithDic:modelDic];
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)createCollectionView{
     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    flowLayout.minimumInteritemSpacing = SPACED;
    flowLayout.minimumLineSpacing = SPACED;
    
    self.collectionView = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 40 - 49) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];  // 颜色
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.contentView addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
    
    // 注册头视图重用池
    [self.collectionView registerClass:[CC_Custom_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

    // 注册尾视图重用池
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    
    // 大图片注册重用池
    [self.collectionView registerClass:[CC_ClassifyHugeImage_CollectionViewCell class] forCellWithReuseIdentifier:IMAGECELL];

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}


// 区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 3;
 
}

// 每个区item的数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 15;
            break;
        default:
            return 10;
            break;
    }
    
}

// 每个区itemsize的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
      
    switch (indexPath.section) {
        case 0:
        return CGSizeMake(SCREENWIDTH , SCREENHEIGHT * 0.3);  // 图片大小
            break;
            
        default:
            return CGSizeMake(SCREENWIDTH - 2 *SPACED, SCREENHEIGHT * 0.2);
            break;
    }
    

}

// 区头大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return CGSizeMake(0, 0);
            break;
            
        default:
            return CGSizeMake(SCREENWIDTH, 45);
            break;
    }
    
    
}
// 区尾大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return CGSizeMake(0, 0);
            break;
            
        default:
             return CGSizeMake(SCREENWIDTH, 15);
            break;
    }
}

/// 返回头视图 和 尾部视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if(kind == UICollectionElementKindSectionHeader)
    {
        
        CC_Custom_CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//        headerView.anchorTitleModel = [self.anchorTitleModelArray objectAtIndex:indexPath.section];
        
        headerView.backgroundColor = [UIColor whiteColor];  // 区头颜色
        
        return headerView;
    }else
    {
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footerView.backgroundColor = [UIColor grayColor];  // 区尾颜色
        footerView.alpha = 0.3;
        return footerView;
 
    }
    
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
  
    
    switch (indexPath.section) {
        case 0:
        {
            CC_ClassifyHugeImage_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IMAGECELL forIndexPath:indexPath];
            cell.topListModel = self.focusImageModel;
            
            return cell; 
        }
            break;
            
        default:
        {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
         cell.backgroundColor = AUTOCOLOR;
       return cell;
        }
            break;
    }
    
    
}





@end
